package cn.trasen.his.poc.std.core.action;

import cn.trasen.his.poc.std.api.model.drug.DrugStock;
import cn.trasen.his.poc.std.api.pmodel.DrugSearchProxy;
import org.springframework.stereotype.Component;
import pro.shushi.pamirs.framework.connectors.data.sql.Pops;
import pro.shushi.pamirs.meta.annotation.Function;
import pro.shushi.pamirs.meta.annotation.Model;
import pro.shushi.pamirs.meta.api.dto.condition.Pagination;
import pro.shushi.pamirs.meta.api.dto.wrapper.IWrapper;
import pro.shushi.pamirs.meta.constant.FunctionConstants;
import pro.shushi.pamirs.meta.enmu.FunctionOpenEnum;
import pro.shushi.pamirs.meta.enmu.FunctionTypeEnum;

import java.util.List;

@Component
@Model.model(DrugSearchProxy.MODEL_MODEL)
public class DrugSearchProxyAction {

    @Function.Advanced(type = FunctionTypeEnum.QUERY)
    @Function.fun(FunctionConstants.queryPage)
    @Function(openLevel = {FunctionOpenEnum.API})
    public Pagination<DrugSearchProxy> queryPage(Pagination<DrugSearchProxy> page, IWrapper<DrugSearchProxy> queryWrapper) {
        Pagination<DrugSearchProxy> pagination = new DrugSearchProxy().queryPage(page, queryWrapper);
        List<DrugSearchProxy> content = pagination.getContent();
        for (DrugSearchProxy drugSearchProxy : content) {
            IWrapper<DrugStock> qw = Pops.<DrugStock>lambdaQuery()
                    .from(DrugStock.MODEL_MODEL)
                    .eq(DrugStock::getDrugId, drugSearchProxy.getId());
            DrugStock drugStock = new DrugStock().queryOneByWrapper(qw);
            drugSearchProxy.setStockNum(drugStock.getStockNum());
        }
        return pagination;
    }

}
